home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Kant Generator Pro 1.2 / src / kode ƒ / kant build files.c < prev    next >
C/C++ Source or Header  |  1995-02-09  |  10KB  |  430 lines

  1. #include "kant build files.h"
  2. #include "kant build window.h"
  3. #include "kant build lists.h"
  4. #include "resource utilities.h"
  5. #include "file interface.h"
  6. #include "file utilities.h"
  7. #include "menus.h"
  8. #include "graphics.h"
  9. #include "window layer.h"
  10. #include "program globals.h"
  11. #include "util.h"
  12. #include <Script.h>
  13. //#include <CursorCtl.h>
  14.  
  15. #define MODULE_FOLDER_NAME    "\pModules ƒ"
  16.  
  17. static    OSErr CreateNewModuleFile(FSSpec *theFS, Boolean deleteTheThing);
  18.  
  19. void UseTheModule(FSSpec *theFS, Boolean useDefault)
  20. {
  21.     gModuleFS=*theFS;
  22.     gUseDefault=useDefault;
  23.     RebuildInsertMenu();
  24. }
  25.  
  26. OSErr GetNamedModuleFS(FSSpec *theFS, Str255 theName)
  27. {
  28.     FSSpec            appFS;
  29.     OSErr            oe;
  30.     CInfoPBRec        pb_dir;
  31.     
  32.     if ((oe=GetApplicationFSSpec(&appFS))!=noErr)
  33.         return oe;
  34.     
  35.     pb_dir.dirInfo.ioCompletion=0L;
  36.     pb_dir.dirInfo.ioNamePtr=MODULE_FOLDER_NAME;
  37.     pb_dir.dirInfo.ioVRefNum=appFS.vRefNum;
  38.     pb_dir.dirInfo.ioFDirIndex=0; /* very important */
  39.     pb_dir.dirInfo.ioDrDirID=appFS.parID;
  40.     if ((oe=PBGetCatInfo(&pb_dir, FALSE))!=noErr)
  41.         return oe;
  42.     
  43.     Mymemcpy((Ptr)theFS->name, (Ptr)theName, theName[0]+1);
  44.     theFS->vRefNum=appFS.vRefNum;
  45.     theFS->parID=pb_dir.dirInfo.ioDrDirID;
  46.     
  47.     return FileExists(theFS);
  48. }
  49.  
  50. OSErr BuildReferencesList(MenuHandle theMenu, FSSpec theFS)
  51. {
  52.     OSErr            oe;
  53.     short            oldRefNum, refNum, numStringResources, resID, i;
  54.     Boolean            alreadyOpen;
  55.     Str255            resName;
  56.     ResType            resType;
  57.     Handle            resHandle;
  58.     short            resAttrs;
  59.     Boolean            gotone;
  60.     
  61.     if ((oe=OpenTheResFile(&theFS, &oldRefNum, &refNum, &alreadyOpen, TRUE))!=noErr)
  62.         return oe;
  63.     
  64.     numStringResources=Count1Resources('STR#');
  65.     gotone=FALSE;
  66.     for (i=1; i<=numStringResources; i++)
  67.     {
  68.         resHandle=Get1IndResource('STR#', i);
  69.         if (resHandle==0L)
  70.             return ResError();
  71.         
  72.         GetResInfo(resHandle, &resID, &resType, resName);
  73.         oe=ResError();
  74.         if (oe!=noErr)
  75.         {
  76.             ReleaseResource(resHandle);
  77.             CloseTheResFile(oldRefNum, refNum, alreadyOpen);
  78.             return oe;
  79.         }
  80.         resAttrs=GetResAttrs(resHandle);
  81.         oe=ResError();
  82.         ReleaseResource(resHandle);
  83.         if (oe!=noErr)
  84.         {
  85.             CloseTheResFile(oldRefNum, refNum, alreadyOpen);
  86.             return oe;
  87.         }
  88.         
  89.         if ((gShowAllRefs) || ((resAttrs&0x00ff) & resLocked))
  90.         {
  91.             AppendMenu(theMenu, "\p ");
  92.             SetItem(theMenu, CountMItems(theMenu), resName);
  93.             gotone=TRUE;
  94.         }
  95.     }
  96.     
  97.     CloseTheResFile(oldRefNum, refNum, alreadyOpen);
  98.     
  99.     if (!gotone)
  100.     {
  101.         AppendMenu(theMenu, gShowAllRefs ? "\p(No references" : "\p(No interesting references");
  102.     }
  103.     
  104.     return noErr;
  105. }
  106.  
  107. OSErr BuildModulesList(MenuHandle theMenu)
  108. {
  109.     FSSpec            appFS;
  110.     OSErr            oe;
  111.     CInfoPBRec        pb_dir;
  112.     HParamBlockRec    pb_file;
  113.     Str255            theName;
  114.     short            i;
  115.     
  116.     if ((oe=GetApplicationFSSpec(&appFS))!=noErr)
  117.         return oe;
  118.     
  119.     pb_dir.dirInfo.ioCompletion=0L;
  120.     pb_dir.dirInfo.ioNamePtr=MODULE_FOLDER_NAME;
  121.     pb_dir.dirInfo.ioVRefNum=appFS.vRefNum;
  122.     pb_dir.dirInfo.ioFDirIndex=0; /* very important */
  123.     pb_dir.dirInfo.ioDrDirID=appFS.parID;
  124.     if ((oe=PBGetCatInfo(&pb_dir, FALSE))!=noErr)
  125.         return oe;
  126.     
  127.     if (pb_dir.dirInfo.ioDrNmFls==0)
  128.         return noErr;
  129.     
  130.     for (i=1; (i<=pb_dir.dirInfo.ioDrNmFls); i++)
  131.     {
  132.         pb_file.fileParam.ioCompletion=0L;
  133.         pb_file.fileParam.ioNamePtr=theName;
  134.         pb_file.fileParam.ioVRefNum=appFS.vRefNum;
  135.         pb_file.fileParam.ioFDirIndex=i;
  136.         pb_file.fileParam.ioDirID=pb_dir.dirInfo.ioDrDirID;
  137.         if ((oe=PBGetCatInfo((CInfoPBPtr)&pb_file, FALSE))==noErr)
  138.         {
  139.             if ((pb_file.fileParam.ioFlFndrInfo.fdType==BUILD_TYPE) &&
  140.                 (pb_file.fileParam.ioFlFndrInfo.fdCreator==CREATOR))
  141.             {
  142.                 /* if theName contains a / character, it will be interpreted as a */
  143.                 /* meta-character by AppendMenu but not by SetItem */
  144.                 AppendMenu(theMenu, "\p ");
  145.                 SetItem(theMenu, CountMItems(theMenu), theName);
  146.             }
  147.         }
  148.     }
  149.     
  150.     return noErr;
  151. }
  152.  
  153. OSErr DoNewModule(void)
  154. {
  155.     FSSpec            theFS;
  156.     Boolean            deleteTheThing;
  157.     OSErr            oe;
  158.     
  159.     theFS.name[0]=0x00;
  160.     if (!GetDestFile(&theFS, &deleteTheThing, "\pSave new module as..."))
  161.         return -1;
  162.     
  163.     if ((oe=CreateNewModuleFile(&theFS, deleteTheThing))!=noErr)
  164.         return oe;
  165.     
  166.     RebuildModulesMenu();
  167.     
  168.     return OpenTheModule(&theFS, TRUE, FALSE);
  169. }
  170.  
  171. OSErr OpenTheModule(FSSpec *theFS, Boolean newModule, Boolean useOldFile)
  172. {
  173.     WindowPtr        buildWindow;
  174.     
  175.     if ((!newModule) && (!useOldFile))
  176.     {
  177.         if (!GetSourceFile(theFS, BUILD_TYPE))
  178.             return -1;
  179.     }
  180.     
  181.     SetBuildWindowTitle(theFS->name);
  182.     OpenTheIndWindow(kBuildWindow);
  183.     buildWindow=GetIndWindowPtr(kBuildWindow);
  184.     SetWindowFS(buildWindow, *theFS);
  185.     UseTheModule(theFS, FALSE);
  186.     BuildReferenceListHandle(GetWindowFS(buildWindow), FALSE);
  187.     BuildInstantListHandle(GetWindowFS(buildWindow), FALSE);
  188.     
  189.     return noErr;
  190. }
  191.  
  192. Boolean ReferenceNameExistsQQ(FSSpec theFS, Str255 refName)
  193. {
  194.     OSErr            oe;
  195.     short            oldRefNum, refNum;
  196.     Boolean            alreadyOpen;
  197.     Handle            resHandle;
  198.     Boolean            exists;
  199.     
  200.     if ((oe=OpenTheResFile(&theFS, &oldRefNum, &refNum, &alreadyOpen, TRUE))!=noErr)
  201.         return FALSE;
  202.     
  203.     resHandle=Get1NamedResource('STR#', refName);
  204.     exists=(resHandle!=0L);
  205.     if (exists)
  206.         ReleaseResource(resHandle);
  207.     CloseTheResFile(oldRefNum, refNum, alreadyOpen);
  208.     
  209.     return exists;
  210. }
  211.  
  212. OSErr DeleteOneReference(FSSpec theFS, Str255 refName)
  213. {
  214.     OSErr            oe;
  215.     short            oldRefNum, refNum;
  216.     Boolean            alreadyOpen;
  217.     Handle            resHandle;
  218.     
  219.     if ((oe=OpenTheResFile(&theFS, &oldRefNum, &refNum, &alreadyOpen, FALSE))!=noErr)
  220.         return oe;
  221.     
  222.     resHandle=Get1NamedResource('STR#', refName);
  223.     if (resHandle==0L)
  224.         return ResError();
  225.     
  226.     RmveResource(resHandle);
  227.     oe=ResError();
  228.     if (oe==noErr)
  229.     {
  230.         UpdateResFile(refNum);
  231.         DisposeHandle(resHandle);    /* correct, this handle is no longer a resource */
  232.     }
  233.     else ReleaseResource(resHandle);
  234.     
  235.     CloseTheResFile(oldRefNum, refNum, alreadyOpen);
  236.     
  237.     return oe;
  238. }
  239.  
  240. OSErr DeleteOneInstantiation(FSSpec theFS, Str255 refName, short stringIndex)
  241. {
  242.     OSErr            oe;
  243.     short            oldRefNum, refNum, resID;
  244.     Boolean            alreadyOpen;
  245.     ResType            resType;
  246.     Handle            resHandle;
  247.     Str255            dummy;
  248.     
  249.     if ((oe=OpenTheResFile(&theFS, &oldRefNum, &refNum, &alreadyOpen, FALSE))!=noErr)
  250.         return oe;
  251.     
  252.     resHandle=Get1NamedResource('STR#', refName);
  253.     if (resHandle!=0L)
  254.     {
  255.         GetResInfo(resHandle, &resID, &resType, dummy);
  256.         ReleaseResource(resHandle);        /* just needed resID */
  257.         
  258.         oe=DeleteIndString(resID, stringIndex);
  259.     }
  260.     else oe=ResError();
  261.     
  262.     CloseTheResFile(oldRefNum, refNum, alreadyOpen);
  263.     
  264.     return oe;
  265. }
  266.  
  267. OSErr AddOneReference(FSSpec theFS, Str255 refName)
  268. {
  269.     OSErr            oe;
  270.     short            oldRefNum, refNum;
  271.     Boolean            alreadyOpen;
  272.     Handle            resHandle;
  273.     
  274.     if ((oe=OpenTheResFile(&theFS, &oldRefNum, &refNum, &alreadyOpen, FALSE))!=noErr)
  275.         return oe;
  276.     
  277.     resHandle=NewHandle(2L);
  278.     **((short**)resHandle)=0;
  279.     AddResource(resHandle, 'STR#', Unique1ID('STR#'), refName);
  280.     oe=ResError();
  281.     
  282.     if (oe==noErr)
  283.     {
  284.         WriteResource(resHandle);
  285.         oe=ResError();
  286.     }
  287.     
  288.     CloseTheResFile(oldRefNum, refNum, alreadyOpen);
  289.     ReleaseResource(resHandle);
  290.     
  291.     return oe;
  292. }
  293.  
  294. OSErr AddOneInstantiation(FSSpec theFS, Str255 refName, Str255 instantName)
  295. {
  296.     OSErr            oe;
  297.     short            oldRefNum, refNum, resID;
  298.     Boolean            alreadyOpen;
  299.     Str255            dummy;
  300.     ResType            resType;
  301.     Handle            resHandle;
  302.     
  303.     if ((oe=OpenTheResFile(&theFS, &oldRefNum, &refNum, &alreadyOpen, FALSE))!=noErr)
  304.         return oe;
  305.     
  306.     resHandle=Get1NamedResource('STR#', refName);
  307.     if (resHandle!=0L)
  308.     {
  309.         GetResInfo(resHandle, &resID, &resType, dummy);
  310.         ReleaseResource(resHandle);        /* just needed resID */
  311.         
  312.         oe=AddIndString(instantName, resID);
  313.     }
  314.     else oe=ResError();
  315.     
  316.     CloseTheResFile(oldRefNum, refNum, alreadyOpen);
  317.     
  318.     return oe;
  319. }
  320.  
  321. OSErr ReplaceOneReference(FSSpec theFS, Str255 oldRef, Str255 newRef)
  322. {
  323.     OSErr            oe;
  324.     short            oldRefNum, refNum, resID;
  325.     Boolean            alreadyOpen;
  326.     Str255            resName;
  327.     ResType            resType;
  328.     Handle            resHandle;
  329.     
  330.     if ((oe=OpenTheResFile(&theFS, &oldRefNum, &refNum, &alreadyOpen, FALSE))!=noErr)
  331.         return oe;
  332.     
  333.     resHandle=Get1NamedResource('STR#', oldRef);
  334.     if (resHandle==0L)
  335.         return ResError();
  336.     
  337.     GetResInfo(resHandle, &resID, &resType, resName);
  338.     SetResInfo(resHandle, resID, newRef);
  339.     oe=ResError();
  340.     if (oe==noErr)
  341.     {
  342.         ChangedResource(resHandle);
  343.         WriteResource(resHandle);
  344.         UpdateResFile(refNum);
  345.         oe=ResError();
  346.     }
  347.     
  348.     ReleaseResource(resHandle);
  349.     CloseTheResFile(oldRefNum, refNum, alreadyOpen);
  350.     
  351.     return oe;
  352. }
  353.  
  354. OSErr ReplaceOneInstantiation(FSSpec theFS, Str255 oldRef, short stringIndex, Str255 newStr)
  355. {
  356.     OSErr            oe;
  357.     short            oldRefNum, refNum, resID;
  358.     Boolean            alreadyOpen;
  359.     Str255            resName;
  360.     ResType            resType;
  361.     Handle            resHandle;
  362.     
  363.     if ((oe=OpenTheResFile(&theFS, &oldRefNum, &refNum, &alreadyOpen, FALSE))!=noErr)
  364.         return oe;
  365.     
  366.     resHandle=Get1NamedResource('STR#', oldRef);
  367.     if (resHandle!=0L)
  368.     {
  369.         GetResInfo(resHandle, &resID, &resType, resName);
  370.         ReleaseResource(resHandle);        /* just needed resID */
  371.         oe=SetIndString(newStr, resID, stringIndex);
  372.     }
  373.     else oe=ResError();
  374.     
  375.     CloseTheResFile(oldRefNum, refNum, alreadyOpen);
  376.     
  377.     return oe;
  378. }
  379.  
  380. OSErr SetInterestingBit(FSSpec theFS, Str255 referenceName, Boolean isInteresting)
  381. {
  382.     OSErr            oe;
  383.     short            oldRefNum, refNum;
  384.     Boolean            alreadyOpen;
  385.     Handle            resHandle;
  386.     short            theAttrs;
  387.     
  388.     if ((oe=OpenTheResFile(&theFS, &oldRefNum, &refNum, &alreadyOpen, FALSE))!=noErr)
  389.         return oe;
  390.     
  391.     resHandle=Get1NamedResource('STR#', referenceName);
  392.     if (resHandle!=0L)
  393.     {
  394.         theAttrs=GetResAttrs(resHandle);
  395.         if (isInteresting)
  396.             theAttrs|=resLocked;
  397.         else
  398.             theAttrs&=~resLocked;
  399.         SetResAttrs(resHandle, theAttrs);
  400.         if ((oe=ResError())==noErr)
  401.         {
  402.             ChangedResource(resHandle);
  403.             oe=ResError();
  404.         }
  405.     }
  406.     else oe=ResError();
  407.     
  408.     CloseTheResFile(oldRefNum, refNum, alreadyOpen);
  409.     
  410.     if (resHandle!=0L)
  411.         ReleaseResource(resHandle);
  412.     
  413.     return oe;
  414. }
  415.  
  416. /* the rest of these are internal to kant build files.c */
  417.  
  418. static    OSErr CreateNewModuleFile(FSSpec *theFS, Boolean deleteTheThing)
  419. {
  420.     OSErr            oe;
  421.     
  422.     if (deleteTheThing)
  423.         if ((oe=FSpDelete(theFS))!=noErr)
  424.             return oe;
  425.     
  426.     FSpCreateResFile(theFS, CREATOR, BUILD_TYPE, smSystemScript);
  427.     
  428.     return ResError();
  429. }
  430.